import { BaseManager } from './../libs/rxjs/cc3/BaseManager';
import { _decorator, color, director, System, Node } from 'cc';
import { lastValueFrom } from 'rxjs';
import { bam } from './BundleAssetsManager';
import { dm } from './DataManager';
import { SmartNodePool } from '../libs/utils/SmartNodePool';
import { Prefab } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('PoolManager')
export class PoolManager extends BaseManager {

    public static instance: PoolManager;
    static ID = 'pool_system';
    get logColorOverride() { return color().fromHEX('#EECCFF') };

    static registerSystem() {
        director.unregisterSystem(director.getSystem(PoolManager.ID));
        director.registerSystem(PoolManager.ID, pm, System.Priority.MEDIUM);
        pm.init();
    }

    onInitManager() {
        this.log('onInitManager', this.timestamp);
    }

    _pools: Record<string, SmartNodePool> = {};

    prepare(prefab: Prefab, initCount: number = 0) {
        const snp = new SmartNodePool();
        snp.setPrefab(prefab);
        snp.prepare(initCount);
        this._pools[prefab.name] = snp;
    }

    getNode(name: string): Node {
        return this._pools[name].getNode();
    }

    putNode(node: Node) {
        this._pools[node.name].putNode(node);
    }

}
globalThis.PoolManager = PoolManager;

export const pm = PoolManager.instance = new PoolManager();